<?php
class Api{
	public static $hostname = [
		'local' => [
			'admin' => 'http://news.com',
			'data'  => 'http://dev.data.bossjob.com',
		],
		'dev' => [
			'admin' => 'http://dev.admin.bossjob.com:8079',
			'data'  => 'http://dev.data.bossjob.com',
		],
		'staging' => [
			'admin' => 'http://staging.admin.bossjob.com:8079',
			'data'  => 'http://staging.data.bossjob.com',
		],
	];

	public static $apiname = [
		'admin' => [
			'getLoginQR' => [
				'url' => '/api/v1/qr/get',
				'data' => [],
			],
			'askLoginResult' => [
				'url' => '/api/v1/qr/ask',
				'data' => [
					'qr_id' => '',
				],
			],
			'qrLogin' => [
				'url' => '/api/v1/qr/qrLogin',
				'data' => [
					'qr_id' => '',
					'jwt_token' => '',
					'user_id' => 0
				],
			],
		],
		'data' => [
			'getSeekerInfo' => [
				'url' => '/api/v1/users/{uid}',
				'data' => [
					'uid' => 1,
				],
			],
			'getRecuiterInfo' => [
				'url' => '/api/v1/recruiters/{uid}',
				'data' => [
					'uid' => 1,
				],
			],
			'getRecruiterFullInfo' => [
				'url' => '/api/v1/admins/recruiters/{ruid}',
				'data' => [
					'ruid' => 1,
				],
			],
		],
	];

	public static function getUrl($system, $apiname, $data=false, $env='local'){
		$domain = self::$hostname[$env][$system];
		$apinameData = self::$apiname[$system][$apiname];
		$apiname = $apinameData['url'];
		$data = $data ? $data : $apinameData['data'];
		foreach ($data as $k => $v) {
			$apiname = str_replace('{'.$k.'}', $v, $apiname);
		}
		return $domain.$apiname;
	}

	public static function post($url, $data){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		// post数据
		curl_setopt($ch, CURLOPT_POST, 1);
		// post的变量
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
		$output = curl_exec($ch);
		curl_close($ch);
		$rtn = json_decode($output, true);
		return $rtn;
	}

	public static function get($url, $json=true){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		$rtn = curl_exec($ch);
		curl_close($ch);
		if($json){
			$rtn = json_decode($rtn, true);
		}
		return $rtn;
	}

	public static function echo($title, $data, $style=[]){
		$style['width'] = isset($style['width']) && $style['width'] ? $style['width'] : '30%';
		echo '<div style="float:left; width:'.$style['width'].';height:auto;margin-left:100px;">';
		echo '<h2>'.$title.'</h2>';
		echo '<textarea style="float:left; width:100%;height:1000px;">';
		print_r($data);
		echo '</textarea>';
		echo '</div>';
		return $data;
	}
}

// Api::echo('用户详情', Api::get(Api::getUrl('data', 'getSeekerInfo', ['uid' => 411], 'staging'), true));
Api::echo(
	'recruiter full info. [url='.Api::getUrl('data', 'getRecruiterFullInfo', ['ruid' => 411], 'staging').']', 
	Api::get(
		Api::getUrl(
			'data', 
			'getRecruiterFullInfo', 
			['ruid' => 411], 
			'staging'
		), 
		true
	), 
	['width'=>'2000px']
);
// Api::echo('recruiters 详情', Api::get(Api::getUrl('data', 'getRecuiterInfo', ['uid' => 411], 'staging'), true));

// $qr = Api::echo('获取qr二维码', Api::get(Api::getUrl('admin', 'getLoginQR', [])), ['width'=>'400px']);
// $login = Api::echo('二维码登录', Api::post(Api::getUrl('admin', 'qrLogin'), ['qr_id'=>$qr['data']['qr_id'], 'jwt_token'=>'test'/*, 'user_id'=>12*/]), ['width'=>'400px']);
// $ask = Api::echo('询问二维码登录结果', Api::post(Api::getUrl('admin', 'askLoginResult'), ['qr_id'=>$qr['data']['qr_id']]), ['width'=>'400px']);

?>